import json
import time

from selenium import webdriver
import requests
from selenium.webdriver.common.by import By

# s = ("1","2","3","4")
#
# for i in range(4):
#     print(s[i])
#

arr = (
"宜昌荆楚物业管理有限公司宜都分公司",
"临汾市汇捷劳务派遣有限公司",
"钟祥市信达电器有限公司",
"恩施自治州众信房地产有限责任公司",
"杭州萧山翠姐姐酒店管理有限公司"
)

js = {
    "company":"",
    "cdoe":""
}

alljs = []

formdata = {
    "type":"AUTO",
    "i":"i love python",
    "doctype":"json",
    "xmlVersion":"1.8",
    "keyfrom":"fanyi.web",
    "ue":"UTF-8",
    "action":"FY_BY_ENTER",
    "typoResult":"true"
}

def deal_req():

    kw = {'wd': '长城'}
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36",
        "":""
    }
    # params 接收一个字典或者字符串的查询参数，字典类型自动转换为url编码，不需要urlencode()
    response = requests.get("http://www.baidu.com/s?", params=kw, headers=headers)



def deal_data() :
    for i in range(216):
        driver = webdriver.Chrome()
        # driver.get("https://www.baidu.com")
        driver.get("https://www.tianyancha.com/")

        driver.set_window_size(8000, 6000)
        driver.find_element(by=By.XPATH,
                            value='//*[@id="page-container"]/div[1]/div/div[3]/div[2]/div[1]/div/input').send_keys(arr[i])
        driver.find_element(by=By.XPATH,
                            value='//*[@id="page-container"]/div[1]/div/div[3]/div[2]/div[1]/button').click()
        # driver.find_element(by=By.XPATH, value='//*[@id="page-container"]/div/div[2]/section/main/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div[1]/a').click()
        stra = driver.find_elements(by=By.XPATH,
                                    value='//*[@id="page-container"]/div/div[2]/section/main/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div[1]/a')
        if len(stra) == 0:
            print(arr[i])
            continue
        url = stra[0].get_attribute('href')
        if url is None:
            print(arr[i])
            continue
        driver.quit()

        time.sleep(3)
        driver2 = webdriver.Chrome()
        driver2.get(url)
        time.sleep(2)
        st = driver2.find_elements(by=By.XPATH,
                                   value='//*[@id="page-root"]/div[3]/div[1]/div[3]/div/div[2]/div[2]/div/div[2]/div/div[2]/table/tbody/tr[5]/td[2]/div/span[1]')
        if len(st) == 0:
            st = driver2.find_elements(by=By.XPATH,
                                       value='//*[@id="page-root"]/div[3]/div/div[3]/div/div[2]/div[2]/div/div[1]/div/div[2]/table/tbody/tr[3]/td[6]/span/span')
        if len(st) == 0:
            print(arr[i])
            continue
        text = st[0].text
        print(text)
        time.sleep(3)
        # j = json.loads(js)
        # j['company'] = arr[i]
        # j['cdoe'] = text
        # alljs.append(js)

deal_data()

# y = json.dumps(alljs, ensure_ascii=False)
# print(y)

# data = {
#     "create_time_begin":1656156762421,
#     "create_time_end":1663932762421,
#     "applyment_state":99,
#     "key":1613067046,
#     "page_num":0,
#     "page_size":10,
#     "ecc_csrf_token":"ece40982c60bcf17df154e62f6352d57"
# }
# url = "https://pay.weixin.qq.com/index.php/core/apply4sub/query_applyment_list"
# r = requests.post(url, data=json.dumps(data), headers="", verify=False, timeout=10)
# r = json.loads(r.text)
# print(r)







